package com.abei.gas.service.Impl;

import com.abei.gas.entity.Device;
import com.abei.gas.entity.Employee;
import com.abei.gas.entity.Inorder;
import com.abei.gas.entity.Warehouse;
import com.abei.gas.mapper.EmployeeMapper;
import com.abei.gas.mapper.InorderMapper;
import com.abei.gas.mapper.WarehouseMapper;
import com.abei.gas.service.InorderService;
import com.abei.gas.vo.DataVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InorderServiceImpl extends ServiceImpl<InorderMapper, Inorder> implements InorderService {
    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private WarehouseMapper warehouseMapper;

    @Resource
    private InorderMapper inorderMapper;

    @Override
    public DataVo stockIn(Inorder inorder) {
        QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.eq("id",inorder.getEmployeeid());
        List<Employee> employeeList = employeeMapper.selectList(employeeQueryWrapper);
        if(employeeList.size()==0){
            return DataVo.error(1,"职工不存在");
        }

        QueryWrapper<Warehouse> warehouseQueryWrapper =new QueryWrapper<>();
        warehouseQueryWrapper.eq("type",inorder.getType());
        List<Warehouse> warehouseList = warehouseMapper.selectList(warehouseQueryWrapper);
        if(warehouseList.size()==0){
            return DataVo.error(1,"仓库不存在");
        }

        inorderMapper.insert(inorder);

        //库存增加
        Warehouse warehouse = warehouseMapper.selectOne(warehouseQueryWrapper);
        warehouse.setCount(warehouse.getCount()+inorder.getCount());
        warehouseMapper.updateById(warehouse);

        return DataVo.success();
    }

    @Override
    public DataVo<Inorder> findAll(Integer page, Integer limit) {
        List<Inorder> c=inorderMapper.selectList(null);
        DataVo<Inorder> datavo=new DataVo<>();
        datavo.setCode(0);
        datavo.setMsg("");
        IPage<Inorder> cardpage=new Page<>(page,limit);
        IPage<Inorder> result=inorderMapper.selectPage(cardpage,null);
        datavo.setCount(result.getTotal());
        c=result.getRecords();
        datavo.setData(c);
        return datavo;
    }

    @Override
    public DataVo<Inorder> findById(String number) {
        QueryWrapper<Inorder> u = new QueryWrapper<>();
        u.like("id",number);
        List<Inorder> cardList=inorderMapper.selectList(u);
        DataVo<Inorder> dataVO=new DataVo<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        Integer num = inorderMapper.selectCount(null);
        dataVO.setCount(Long.valueOf(num));
        dataVO.setData(cardList);
        return dataVO;
    }

    @Override
    public void deleteById(String id) {
        Map<String,Object> columMap=new HashMap<>();
        columMap.put("id",id);
        int i=inorderMapper.deleteByMap(columMap);
        System.out.println(i);
    }

}
